# import pymysql
# import time
#
#
# # 密码要求(新增、修改)：
# # 1.长度超过8位
# # 2.包括大、小写字母.数字.其它,以上四种至少三种
# def check_pwd(pwd):
#     if len(pwd) <= 8:
#         print('密码过短')
#     else:
#         a = b = c = d = 0
#         for i in pwd:
#             if i.isupper():
#                 a = 1
#             elif i.islower():
#                 b = 1
#             elif i.isdigit():
#                 c = 1
#             else:
#                 d = 1
#         if a + b + c + d >= 3:
#             return '密码格式正确'
#         else:
#             return '密码格式不正确'
#
#
# def have_user(usr):
#     """
#     :param usr:
#     :return: 存在用户返回 True ,不存在 返回False
#     """
#     if 6 <= len(usr) <= 20:
#         # 前提
#         data = mysql(text=f"select * from user where uname = '{usr}'")
#         if len(data) == 0:
#             return False
#         else:
#             return True
#     else:
#         return False
#
#
# # 2、用户id 自动生成，唯一
# def make_userid():
#     uid = int(time.time() * 1000000)
#     return uid
#
#
# # 1、用户系统 数据 存入数据库
# def mysql(text):
#     sql = pymysql.connect(
#         user='test',  # 用户名
#         password='test123',  # 密码
#         host='192.168.0.125',  # 数据库ip
#         port=3306,  # 数据库端口
#         database="userinfo",  # 数据库名称
#         charset='utf8mb4')  # 字符编码
#     cs = sql.cursor()
#     text = 'select * from question;'
#     cs.execute(text)
#     data = cs.fetchall()
#     sql.commit()
#     sql.close()
#     return data
#
#
# msg = """欢迎来到智商充值用户系统，请输入您的操作指令
# 0 >>> 退出
# 1 >>> 注册
# 2 >>> 登录
# 3 >>> 修改密码
# 4 >>> 删除用户"""
# print(msg)
#
# while True:
#     zl = input('请输入您的指令')
#     if zl == 0:
#         break
#     elif zl == 1:  # 用户名 长度 必须在6-20位之间，删除用户，必须校验用户密码
#         username = input('请输入用户名')
#         if 6 <= len(username) <= 20:
#             data = mysql(f'select * from userinfo where name = {username}')
#             if len(data) == 0:
#                 password = input('请输入密码')
#                 if check_pwd(pwd=password):
#                     uid = make_userid()
#                     mysql(f"insert into user values (uid,'{username}','{password}')")
#                 else:
#                     print('密码格式不正确')
#             else:
#                 print('用户已存在')
#         else:
#             print('用户名格式不正确')
#     elif zl == 2:
#         username = input('请输入用户名')
#         data = mysql(text=f"select * from user where name = '{username}';")
#         if len(data) == 0:
#             print('用户尚未注册')
#         else:
#             sql_password = mysql(f"select upassword from user where name = '{username}';")
#             if password == sql_password[0][0]:
#                 print('登陆成功')
#             else:
#                 print('登录失败，账号或密码错误')
#     elif zl == 3:   # 修改密码，需要输入原密码与新密码，且新密码不能与旧密码一致
#         password = input('请输入密码:')
#         data = mysql(f"select upassword from user where uname ='{username}'")
#         if password != data[0][0]:
#             print('密码错误')
#         else:
#             npassword = input('请输入新密码')
#             if npassword == data[0][0]:
#                 print('新密码不能与旧密码一致')
#             else:
#                 mysql(f"update user set upassword ='{npassword}' where uname ='{username}'")
#                 print('密码修改成功')
#
#     elif zl == 4:
#         username = input('请输入用户名:')
#         data = mysql(f"select * from user where uname ='{username}';")
#         if len(data) == 0:
#             # 没有该用户
#             print('用户名不存在')
#         else:
#             password = input('请输入密码:')
#             data = mysql(f"select upassword from user where uname ='{username}'")
#             if password != data[0][0]:
#                 print('密码错误')
#             else:
#                 mysql(f"delete from user where uname ='{username}'")
#                 print('用户删除成功')
#     else:
#         print('输入指令错误')

# from selenium import webdriver
# from selenium.webdriver.common.by import By
# import time
# d = webdriver.Chrome()
# d.get('http://192.168.0.125:9009/wp-login.php')
# time.sleep(3)
# name_ele = d.find_element(By.XPATH,'//input[@name="log"]')
# name_ele.send_keys('admin')
# time.sleep(2)
# password_ele = d.find_element(By.XPATH,'//input[@name="pwd"]')
# password_ele.send_keys('admin123')
# time.sleep(2)
# butten_ele = d.find_element(By.XPATH, '//input[@name="wp-submit"]')
# butten_ele.click()
# time.sleep(3)
# d.quit()

# from selenium import webdriver
# from selenium.webdriver.common.by import By
# import time
#
# d = webdriver.Chrome()
# d.get('http://192.168.0.125:9009/wp-login.php')
# time.sleep(3)
# name_ele = d.find_element(By.CSS_SELECTOR, 'input[name="log"]')
# name_ele.send_keys('admin')
# time.sleep(2)
# password_ele = d.find_element(By.CSS_SELECTOR, 'input[name="pwd"]')
# password_ele.send_keys('admin123')
# time.sleep(2)
# butten_ele = d.find_element(By.CSS_SELECTOR, 'input[class="button button-primary button-large"]')
# time.sleep(2)
# d.quit()


from selenium.webdriver.common.by import By
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
d = webdriver.Chrome()
d.get('https://www.qq.com')
d.maximize_window()
time.sleep(2)
yang1 = d.find_element(By.CSS_SELECTOR,'[class="news-top"] a')
res = yang1.text
yang1.click()
all_windows = d.window_handles
d.switch_to.window(all_windows[1])
content = d.find_element(By.CSS_SELECTOR,'[class="article-wrapper"]')
res2 = content.text
print(res2)
time.sleep(2)

d.get('http://192.168.0.125:9009/')
js = "window.scrollTo(0,1000)"
d.execute_script(js)
login = d.find_element(By.LINK_TEXT, '登录')
login.click()
time.sleep(2)
username_ele = d.find_element(By.CSS_SELECTOR, '[name="log"]')
username_ele.send_keys('admin')
password_ele = d.find_element(By.CSS_SELECTOR, '[name="pwd"]')
password_ele.send_keys('admin123')
button_ele = d.find_element(By.CSS_SELECTOR,'[name="wp-submit"]')
button_ele.click()
time.sleep(2)
yang2_ele = d.find_element(By.CSS_SELECTOR,'[id="menu-posts"] [class="wp-menu-name"]')
ActionChains(d).move_to_element(yang2_ele).perform()
time.sleep(2)
write_ele = d.find_element(By.LINK_TEXT,'写文章')
write_ele.click()
title_ele = d.find_element(By.CSS_SELECTOR, '[name="post_title"]')
title_ele.send_keys(res)
ele = d.find_element(By.CSS_SELECTOR,'[id="content_ifr"]')
d.switch_to.frame(ele)
time.sleep(2)
content_ele = d.find_element(By.CSS_SELECTOR, '[id="tinymce"]')
content_ele.send_keys(res2)
d.switch_to.default_content()
pub = d.find_element(By.CSS_SELECTOR,'[id="publish"]')
js2 = "window.scrollTo(0,0)"
d.execute_script(js2)
pub.click()
time.sleep(3)